/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HomePage.java
 *
 * Created on 2011-6-20, 15:13:18
 */
package seso;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import java.awt.Image;

/**
 *
 * @author Administrator
 */
public class HomePage extends javax.swing.JFrame {

    public String userName = "", dataProfile = "";
    RMiniProgJPanel rMiniProgJPanelX;
    // Program index to identity each dialog entered by user
    int indexMiniProgID = -1;

    public HomePage() {
        super();
        contructClass();
    }

    public HomePage(String userName, String dataProfile) {
        super();

        this.userName = userName;
        this.dataProfile = dataProfile;
        contructClass();
    }

    private void contructClass() {
        initComponents();
        setControl();
    }

    private void setControl() {

        if (userName.length() > 8) {
            userNameJLabel.setText(userName.toLowerCase().substring(0, 8));
        } else {
            userNameJLabel.setText(userName.toLowerCase());
        }
        cmdLogOff.setLocation(cmdLogOff.getX() + (userNameJLabel.getText().trim().length() - 2) * 6, cmdLogOff.getY());

        rHomeStartAndSearch.setRelatedButton(cmdStartAndStart, cmdClearEntrySS);
        rHomeStartAndSearch.setHomePage(this);

        setIconImage(Util.getLogoImage());
        setTitle("SE Smart Office");

        setControlsLocationAndSize();

        cmdHomeHelp.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        homePageTopJPanel = new javax.swing.JPanel();
        miniProgListJPanel = new javax.swing.JPanel();
        homePageToolsJPanel = new javax.swing.JPanel();
        cmdHomeHelp = new seso.RBgButton();
        cmdStartAndStart = new seso.RBgButton();
        cmdClearEntrySS = new seso.RBgButton();
        rHomeStartAndSearch = new seso.RHomeStartAndSearch();
        nameLogoJLabel = new javax.swing.JLabel();
        userNameJLabel = new javax.swing.JLabel();
        cmdLogOff = new seso.RBgButton();
        cmdShowCanvas = new seso.RBgButton();
        cmdFindWindow = new seso.RBgButton();
        cmdShowMore = new seso.RBgButton();
        homeVerticalLineJLabel = new javax.swing.JLabel();
        cmdMin = new seso.RBgButton();
        cmdClose = new seso.RBgButton();
        homePageBottomJPanel = new javax.swing.JPanel();
        homeBackgroupPicture = new seso.RHomeBackgroupPicture();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(seso.SesoApp.class).getContext().getResourceMap(HomePage.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setUndecorated(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        addWindowStateListener(new java.awt.event.WindowStateListener() {
            public void windowStateChanged(java.awt.event.WindowEvent evt) {
                formWindowStateChanged(evt);
            }
        });
        getContentPane().setLayout(null);

        homePageTopJPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        homePageTopJPanel.setName("homePageTopJPanel"); // NOI18N
        homePageTopJPanel.setOpaque(false);
        homePageTopJPanel.setLayout(null);

        miniProgListJPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        miniProgListJPanel.setFocusable(false);
        miniProgListJPanel.setName("miniProgListJPanel"); // NOI18N
        miniProgListJPanel.setOpaque(false);
        miniProgListJPanel.setPreferredSize(new java.awt.Dimension(349, 75));
        homePageTopJPanel.add(miniProgListJPanel);
        miniProgListJPanel.setBounds(0, 39, 230, 74);

        homePageToolsJPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        homePageToolsJPanel.setName("homePageToolsJPanel"); // NOI18N
        homePageToolsJPanel.setOpaque(false);
        homePageToolsJPanel.setLayout(null);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(seso.SesoApp.class).getContext().getActionMap(HomePage.class, this);
        cmdHomeHelp.setAction(actionMap.get("showAboutBox")); // NOI18N
        cmdHomeHelp.setIcon(resourceMap.getIcon("cmdHomeHelp.icon")); // NOI18N
        cmdHomeHelp.setToolTipText(resourceMap.getString("cmdHomeHelp.toolTipText")); // NOI18N
        cmdHomeHelp.setFocusable(false);
        cmdHomeHelp.setName("cmdHomeHelp"); // NOI18N
        cmdHomeHelp.setPressedIcon(resourceMap.getIcon("cmdHomeHelp.pressedIcon")); // NOI18N
        cmdHomeHelp.setRolloverIcon(resourceMap.getIcon("cmdHomeHelp.rolloverIcon")); // NOI18N
        homePageToolsJPanel.add(cmdHomeHelp);
        cmdHomeHelp.setBounds(630, 8, 32, 23);

        cmdStartAndStart.setAction(actionMap.get("cmdStartAndStartAction")); // NOI18N
        cmdStartAndStart.setIcon(resourceMap.getIcon("cmdStartAndStart.icon")); // NOI18N
        cmdStartAndStart.setFocusable(false);
        cmdStartAndStart.setName("cmdStartAndStart"); // NOI18N
        cmdStartAndStart.setRolloverIcon(resourceMap.getIcon("cmdStartAndStart.rolloverIcon")); // NOI18N
        homePageToolsJPanel.add(cmdStartAndStart);
        cmdStartAndStart.setBounds(510, 10, 20, 20);

        cmdClearEntrySS.setAction(actionMap.get("cmdClearEntrySSAction")); // NOI18N
        cmdClearEntrySS.setIcon(resourceMap.getIcon("cmdClearEntrySS.icon")); // NOI18N
        cmdClearEntrySS.setFocusable(false);
        cmdClearEntrySS.setName("cmdClearEntrySS"); // NOI18N
        cmdClearEntrySS.setRolloverIcon(resourceMap.getIcon("cmdClearEntrySS.rolloverIcon")); // NOI18N
        homePageToolsJPanel.add(cmdClearEntrySS);
        cmdClearEntrySS.setBounds(530, 10, 20, 20);

        rHomeStartAndSearch.setFocusable(false);
        rHomeStartAndSearch.setName("rHomeStartAndSearch"); // NOI18N
        homePageToolsJPanel.add(rHomeStartAndSearch);
        rHomeStartAndSearch.setBounds(380, 8, 172, 24);

        nameLogoJLabel.setIcon(resourceMap.getIcon("nameLogoJLabel.icon")); // NOI18N
        nameLogoJLabel.setText(resourceMap.getString("nameLogoJLabel.text")); // NOI18N
        nameLogoJLabel.setName("nameLogoJLabel"); // NOI18N
        nameLogoJLabel.setOpaque(true);
        homePageToolsJPanel.add(nameLogoJLabel);
        nameLogoJLabel.setBounds(4, 6, 90, 27);

        userNameJLabel.setFont(resourceMap.getFont("userNameJLabel.font")); // NOI18N
        userNameJLabel.setForeground(resourceMap.getColor("userNameJLabel.foreground")); // NOI18N
        userNameJLabel.setText(resourceMap.getString("userNameJLabel.text")); // NOI18N
        userNameJLabel.setName("userNameJLabel"); // NOI18N
        homePageToolsJPanel.add(userNameJLabel);
        userNameJLabel.setBounds(112, 11, 80, 20);

        cmdLogOff.setAction(actionMap.get("cmdLogOffAction")); // NOI18N
        cmdLogOff.setIcon(resourceMap.getIcon("cmdLogOff.icon")); // NOI18N
        cmdLogOff.setText(resourceMap.getString("cmdLogOff.text")); // NOI18N
        cmdLogOff.setFocusable(false);
        cmdLogOff.setName("cmdLogOff"); // NOI18N
        cmdLogOff.setPressedIcon(resourceMap.getIcon("cmdLogOff.pressedIcon")); // NOI18N
        cmdLogOff.setRolloverIcon(resourceMap.getIcon("cmdLogOff.rolloverIcon")); // NOI18N
        homePageToolsJPanel.add(cmdLogOff);
        cmdLogOff.setBounds(138, 9, 91, 23);

        cmdShowCanvas.setAction(actionMap.get("cmdShowCanvasAction")); // NOI18N
        cmdShowCanvas.setIcon(resourceMap.getIcon("cmdShowCanvas.icon")); // NOI18N
        cmdShowCanvas.setText(resourceMap.getString("cmdShowCanvas.text")); // NOI18N
        cmdShowCanvas.setFocusable(false);
        cmdShowCanvas.setName("cmdShowCanvas"); // NOI18N
        cmdShowCanvas.setPressedIcon(resourceMap.getIcon("cmdShowCanvas.pressedIcon")); // NOI18N
        cmdShowCanvas.setRolloverIcon(resourceMap.getIcon("cmdShowCanvas.rolloverIcon")); // NOI18N
        homePageToolsJPanel.add(cmdShowCanvas);
        cmdShowCanvas.setBounds(260, 9, 23, 23);

        cmdFindWindow.setIcon(resourceMap.getIcon("cmdFindWindow.icon")); // NOI18N
        cmdFindWindow.setText(resourceMap.getString("cmdFindWindow.text")); // NOI18N
        cmdFindWindow.setFocusable(false);
        cmdFindWindow.setName("cmdFindWindow"); // NOI18N
        cmdFindWindow.setPressedIcon(resourceMap.getIcon("cmdFindWindow.pressedIcon")); // NOI18N
        cmdFindWindow.setRolloverIcon(resourceMap.getIcon("cmdFindWindow.rolloverIcon")); // NOI18N
        homePageToolsJPanel.add(cmdFindWindow);
        cmdFindWindow.setBounds(290, 9, 23, 23);

        cmdShowMore.setAction(actionMap.get("cmdShowMoreAction")); // NOI18N
        cmdShowMore.setIcon(resourceMap.getIcon("cmdShowMore.icon")); // NOI18N
        cmdShowMore.setText(resourceMap.getString("cmdShowMore.text")); // NOI18N
        cmdShowMore.setFocusable(false);
        cmdShowMore.setName("cmdShowMore"); // NOI18N
        cmdShowMore.setPressedIcon(resourceMap.getIcon("cmdShowMore.pressedIcon")); // NOI18N
        cmdShowMore.setRolloverIcon(resourceMap.getIcon("cmdShowMore.rolloverIcon")); // NOI18N
        homePageToolsJPanel.add(cmdShowMore);
        cmdShowMore.setBounds(560, 8, 58, 23);

        homeVerticalLineJLabel.setIcon(resourceMap.getIcon("homeVerticalLineJLabel.icon")); // NOI18N
        homeVerticalLineJLabel.setName("homeVerticalLineJLabel"); // NOI18N
        homeVerticalLineJLabel.setOpaque(true);
        homePageToolsJPanel.add(homeVerticalLineJLabel);
        homeVerticalLineJLabel.setBounds(670, 7, 4, 24);

        cmdMin.setAction(actionMap.get("cmdMinAction")); // NOI18N
        cmdMin.setIcon(resourceMap.getIcon("cmdMin.icon")); // NOI18N
        cmdMin.setToolTipText(resourceMap.getString("cmdMin.toolTipText")); // NOI18N
        cmdMin.setFocusable(false);
        cmdMin.setName("cmdMin"); // NOI18N
        cmdMin.setRolloverIcon(resourceMap.getIcon("cmdMin.rolloverIcon")); // NOI18N
        homePageToolsJPanel.add(cmdMin);
        cmdMin.setBounds(740, 10, 21, 21);

        cmdClose.setAction(actionMap.get("quit")); // NOI18N
        cmdClose.setIcon(resourceMap.getIcon("cmdClose.icon")); // NOI18N
        cmdClose.setText(resourceMap.getString("cmdClose.text")); // NOI18N
        cmdClose.setToolTipText(resourceMap.getString("cmdClose.toolTipText")); // NOI18N
        cmdClose.setFocusable(false);
        cmdClose.setName("cmdClose"); // NOI18N
        cmdClose.setRolloverIcon(resourceMap.getIcon("cmdClose.rolloverIcon")); // NOI18N
        homePageToolsJPanel.add(cmdClose);
        cmdClose.setBounds(770, 10, 21, 21);

        homePageTopJPanel.add(homePageToolsJPanel);
        homePageToolsJPanel.setBounds(0, 0, 800, 38);

        getContentPane().add(homePageTopJPanel);
        homePageTopJPanel.setBounds(0, 0, 830, 114);

        homePageBottomJPanel.setBackground(resourceMap.getColor("homePageBottomJPanel.background")); // NOI18N
        homePageBottomJPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        homePageBottomJPanel.setName("homePageBottomJPanel"); // NOI18N
        homePageBottomJPanel.setOpaque(false);
        homePageBottomJPanel.setLayout(null);
        getContentPane().add(homePageBottomJPanel);
        homePageBottomJPanel.setBounds(170, 160, 230, 70);

        homeBackgroupPicture.setText(resourceMap.getString("homeBackgroupPicture.text")); // NOI18N
        homeBackgroupPicture.setName("homeBackgroupPicture"); // NOI18N
        getContentPane().add(homeBackgroupPicture);
        homeBackgroupPicture.setBounds(10, 150, 140, 80);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void setControlsLocationAndSize() {
        this.setLocation(0, 0);

        //this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setSize(screenWidth, screenHeight - 30);

        int width = this.getWidth();
        int height = this.getHeight();

        homePageTopJPanel.setBorder(null);
        homePageToolsJPanel.setBorder(null);
        homePageBottomJPanel.setBorder(null);
        miniProgListJPanel.setBorder(null);
        homePageTopJPanel.setSize(width, homePageTopJPanel.getHeight());
        homePageToolsJPanel.setSize(width, homePageToolsJPanel.getHeight());
        homePageBottomJPanel.setBounds(0, homePageTopJPanel.getHeight() + 1, width, height - homePageTopJPanel.getHeight());

        homeBackgroupPicture.setLocation(0, 0);
        homeBackgroupPicture.setSize(width, height);

        cmdClose.setLocation(width - cmdClose.getWidth() - 4, cmdClose.getY());
        cmdMin.setLocation(cmdClose.getX() - cmdClose.getWidth() + 2, cmdClose.getY());
        homeVerticalLineJLabel.setLocation(cmdMin.getX() - homeVerticalLineJLabel.getWidth() - 2, homeVerticalLineJLabel.getY());
        cmdHomeHelp.setLocation(homeVerticalLineJLabel.getX() - cmdHomeHelp.getWidth() - 4, cmdHomeHelp.getY());
        cmdShowMore.setLocation(cmdHomeHelp.getX() - cmdShowMore.getWidth(), cmdShowMore.getY());
        cmdFindWindow.setLocation(width - 378, cmdFindWindow.getY());
        cmdShowCanvas.setLocation(cmdFindWindow.getX() - cmdShowCanvas.getWidth() + 1, cmdShowCanvas.getY());
        rHomeStartAndSearch.setLocation(width - 331, rHomeStartAndSearch.getY());
        cmdClearEntrySS.setLocation(rHomeStartAndSearch.getX() + rHomeStartAndSearch.getWidth() - cmdClearEntrySS.getWidth() - 2,
                rHomeStartAndSearch.getY() + 1);
        cmdStartAndStart.setLocation(cmdClearEntrySS.getX() - cmdStartAndStart.getWidth(), cmdClearEntrySS.getY());

    }

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        setControlsLocationAndSize();
    }//GEN-LAST:event_formWindowOpened

    private void formWindowStateChanged(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowStateChanged
        setControlsLocationAndSize();

    }//GEN-LAST:event_formWindowStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new HomePage().setVisible(true);
            }
        });
    }

    @org.jdesktop.application.Action
    public void cmdMinAction() {
        this.setExtendedState(JFrame.ICONIFIED);
    }

    @org.jdesktop.application.Action
    public void showAboutBox() {
        SesoApp.getApplication().show(new SesoAboutBox(this));
    }

    @Action
    public void addMiniProg(javax.swing.JDialog dlg, int originalX, int originalY, String progID, String ProgDescription, Image dialogImage) {

        rMiniProgJPanelX = new RMiniProgJPanel(dlg, originalX, originalY, indexMiniProgID, progID, ProgDescription, dialogImage);
        rMiniProgJPanelX.setName("rMX" + indexMiniProgID);
        rMiniProgJPanelX.setHomePageBottomJPanel(homePageBottomJPanel);

        miniProgListJPanel.add(rMiniProgJPanelX);

        rMiniProgJPanelX.setVisible(false);
        miniProgListJPanel.repaint();
        rMiniProgJPanelX.setVisible(true);
        miniProgListJPanel.setSize(miniProgListJPanel.getComponentCount() * (rMiniProgJPanelX.getPreferredSize().width + 5), miniProgListJPanel.getHeight());
        miniProgListJPanel.repaint();
    }

    public void replaceMiniProg(int indexMiniProgID, javax.swing.JDialog dlg, int originalX, int originalY, String progID, String ProgDescription, Image dialogImage) {

        for (int i = 0; i < miniProgListJPanel.getComponentCount(); i++) {
            if ((miniProgListJPanel.getComponent(i).getName()).equals("rMX" + indexMiniProgID)) {
                ((RMiniProgJPanel) (miniProgListJPanel.getComponent(i))).replaceRMiniProgJPanel(dlg, originalX, originalY, indexMiniProgID, progID, ProgDescription, dialogImage);
                break;
            }
        }

        miniProgListJPanel.repaint();
    }

    @Action
    public void cmdLogOffAction() {
        dispose();
        SesoApp.getApplication().show(new Logon());
    }

    @Action
    public void cmdClearEntrySSAction() {
        rHomeStartAndSearch.setText("");
        rHomeStartAndSearch.setOldProgramID("");
        rHomeStartAndSearch.setCurProgramID("");
        rHomeStartAndSearch.requestFocus();
    }

    @Action
    public void cmdShowMoreAction() {
    }

    @Action
    public void cmdShowCanvasAction() {

        for (int i = 0; i < miniProgListJPanel.getComponentCount(); i++) {
            ((RMiniProgJPanel) (miniProgListJPanel.getComponent(i))).dlg.setVisible(false);
        }
    }

    @Action
    public void cmdStartAndStartAction() {

        MaintDialog dlg = null;

        String programID = rHomeStartAndSearch.getCurProgramID().trim().toUpperCase();
        boolean correctProgramID = false;

        if (programID.equals("SES100")) {
            indexMiniProgID++;
            dlg = new SES100B(this, false);
            correctProgramID = true;
        } else if (programID.equals("SES110")) {
            indexMiniProgID++;
            dlg = new SES110B(this, false);
            correctProgramID = true;
        } else if (programID.equals("SES190")) {
            indexMiniProgID++;
            dlg = new SES190B(this, false);
            correctProgramID = true;
        }  else if (programID.equals("MMS001")) {
            indexMiniProgID++;
            dlg = new MMS001B(this, false);
            correctProgramID = true;
        } else if (programID.equals("SES000")) {
            indexMiniProgID++;
            dlg = new SES000B(this, false);
            correctProgramID = true;
        } 


        if (correctProgramID) {
            rHomeStartAndSearch.setOldProgramID(rHomeStartAndSearch.getCurProgramID());
            dlg.setMiniProgListJPanel(miniProgListJPanel, indexMiniProgID);
            SesoApp.getApplication().show(dlg);
        } else {
            JOptionPane.showMessageDialog(null, "NOT found progam : " + programID);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private seso.RBgButton cmdClearEntrySS;
    private seso.RBgButton cmdClose;
    private seso.RBgButton cmdFindWindow;
    private seso.RBgButton cmdHomeHelp;
    private seso.RBgButton cmdLogOff;
    private seso.RBgButton cmdMin;
    private seso.RBgButton cmdShowCanvas;
    private seso.RBgButton cmdShowMore;
    private seso.RBgButton cmdStartAndStart;
    private seso.RHomeBackgroupPicture homeBackgroupPicture;
    public javax.swing.JPanel homePageBottomJPanel;
    private javax.swing.JPanel homePageToolsJPanel;
    private javax.swing.JPanel homePageTopJPanel;
    private javax.swing.JLabel homeVerticalLineJLabel;
    private javax.swing.JPanel miniProgListJPanel;
    private javax.swing.JLabel nameLogoJLabel;
    private seso.RHomeStartAndSearch rHomeStartAndSearch;
    private javax.swing.JLabel userNameJLabel;
    // End of variables declaration//GEN-END:variables
}
